package com.iamteer.contact.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iamteer.contact.dao.impl.ContactDaoImpl;
import com.iamteer.contact.entity.Contact;

/**
 * Servlet implementation class QueryContact
 */
@WebServlet("/QueryContact")
public class QueryContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String id = request.getQueryString();	//contact id
		ContactDaoImpl dao = new ContactDaoImpl();
		Contact contact = dao.findById(id);
		if (contact == null) {
			response.sendRedirect(getServletContext().getContextPath() + "/ListContact");
			return;
		}
		String html = "";
		html += "<!DOCTYPE html>";
		html += "<html>";
		html += "	<head>";
		html += "		<meta charset='UTF-8'>";
		html += "		<title>修改联系人</title>";
		html += "		<style type='text/css'>";
		html += "			/*文字居中*/";
		html += "			table td{";
		html += "				text-align: center;";
		html += "			}";
		html += "			/*合并表格边框*/";
		html += "			table{";
		html += "				border-collapse: collapse;";
		html += "			}";
		html += "		</style>";
		html += "	</head>";
		html += "	<body>";
		html += "		<center><h3>修改联系人</h3></center>";
		html += "		<form action='/day13Contact/UpdateContact' method='post'>";
		html += "		<table align='center' border='1'>";
		
//		html += "			<tr>";
//		html += "				<th>编号</th>";
//		html += "				<td><input type='text' name='id' value='"+id+"' readonly='true'/></td>";
//		html += "			</tr>";
		
		html += "				<td><input type='hidden' name='id' value='"+id+"'/></td>";
		
		html += "			<tr>";
		html += "				<th>姓名</th>";
		html += "				<td><input type='text' name='name' value='"+contact.getName()+"'/></td>";
		html += "			</tr>";
		html += "			<tr>";
		html += "				<th>性别</th>";
		if ("男".equals(contact.getGender())) {
			html += "				<td><input type='radio' name='gender' value='男' checked='checked'/>男";
			html += "				&nbsp;&nbsp;";
			html += "				<input type='radio' name='gender' value='女'/>女</td>";
		} else {
			html += "				<td><input type='radio' name='gender' value='男'/>男";
			html += "				&nbsp;&nbsp;";
			html += "				<input type='radio' name='gender' value='女' checked='checked'/>女</td>";
		}
		html += "			</tr>";
		html += "			<tr>";
		html += "				<th>年龄</th>";
		html += "				<td><input type='text' name='age' value='"+ contact.getAge() + "'/></td>";
		html += "			</tr>";
		html += "			<tr>";
		html += "				<th>电话</th>";
		html += "				<td><input type='text' name='phone' value='"+ contact.getPhone() + "'/></td>";
		html += "			</tr>";
		html += "			<tr>";
		html += "				<th>邮箱</th>";
		html += "				<td><input type='text' name='email' value='"+ contact.getEmail() + "'/></td>";
		html += "			</tr>";
		html += "			<tr>";
		html += "				<th>QQ</th>";
		html += "				<td><input type='text' name='qq' value='"+ contact.getQq() + "'/></td>";
		html += "			</tr>";
		html += "			<tr>";
		html += "				<td colspan='2'>";
		html += "					<input type='submit' value='保存'/>";
		html += "					&nbsp;&nbsp;";
		html += "					<input type='reset' value='重置'/>";
		html += "				</td>";
		html += "			</tr>";
		html += "		</table>";
		html += "		</form>";
		html += "	</body>";
		html += "</html>";
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(html);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
